<?php

namespace app\common\model\chat;

use think\Model;

class ChatGroup extends Model
{
    protected $append = ['message_num'];

    /**
     * 关联消息
     */
    public function message()
    {
        return $this->hasMany('ChatMessage', 'group_id', 'id');
    }

    /**
     * 删除后操作
     */
    public static function onAfterDelete($model)
    {
        $model->message()->delete();
    }

    /**
     * 获取多少条对话
     */
    public function getMessageNumAttr($value, $data)
    {
        return $this->message()->count();
    }
}